
$(document).ready(onloadProfile);

/**
 * init
 */
function onloadProfile() {
    
    $("#button-save-new-pn").click(savePublicName);
    $("#button-save-new-pn").keypress(function(event) { // jQuery.Event
        if (event.charCode == 13) // enter
            savePublicName();
    });

    $("#button-save-new-pass").click(saveNewPass);
    $("#form-change-pass").keypress(function(event) { // jQuery.Event
        console.debug(event.charCode);
        if (event.charCode == 13) // enter
            saveNewPass();
    });
    
    $('#label-current-pn-msg-update').hide();
    $('#label-pass-update').hide();

} // ! onloadProfile


function savePublicName() {

    $('#label-current-pn-msg-update').hide();
    
    var newPublicName = $('#text-new-pn').val();
    
    console.debug('new publicname: ' + $('#new-public-name').val());
    var url = readCookie("baseurl") + "/profile/savepublicname/publicname/" + newPublicName;
    
    $.ajax({
        type : "GET",
        url : url,
        success : function(msg) {
//            console.debug('savePublicName res: $o', msg);
            if (msg.success) {
                console.debug('success');
                $('#text-new-pn').val('');
                $('#label-current-pn').text(newPublicName);
                $('#label-current-pn-msg-update').css('color', '#0b0');
            }
            else
                $('#label-current-pn-msg-update').css('color', '#d00');
            $('#label-current-pn-msg-update').show();
            $('#label-current-pn-msg-update').text(msg.msg);
        },
    });
    
} // ! savePublicName


function saveNewPass() {
    
    $('#label-pass-update').hide();
    
    if ($('#new-pass-1').val() != $('#new-pass-2').val()) {
        $('#label-pass-update').show();
        $('#label-pass-update').text('Les 2 mots de passe ne correspondent pas');
        $('#label-pass-update').css('color', '#d00');
        return;
    }

    if ($('#old-pass').val() == '' || $('#new-pass-1').val() == '' || $('#new-pass-2').val() == '') {
        $('#label-pass-update').show();
        $('#label-pass-update').text('Champs vides');
        $('#label-pass-update').css('color', '#d00');
        return;
    }
    
    var url = readCookie("baseurl") + "/profile/changepass";
    url += "/oldpass/" + $('#old-pass').val();
    url += "/newpass/" + $('#new-pass-1').val();
    
    $.ajax({
        type : "GET",
        url : url,
        success : function(msg) {
            console.debug('saveNewPass res: %o', msg);
            if (msg.success) {
                $('#new-pass-1').val('');
                $('#new-pass-2').val('');
                $('#label-pass-update').css('color', '#0b0');
            }
            else
                $('#label-pass-update').css('color', '#d00');
            $('#label-pass-update').show();
            $('#label-pass-update').text(msg.msg);
        },
    });    
} // ! saveNewPass
